#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/lexical_cast.hpp>
#include<algorithm>

//#include <regex>
#include "Object.h"
#include "Global.h"
#include "Common.h"
namespace RisingStar
{
	
	CObject::CObject(TObjectProperty in):mObjectProperty(in)
	{
		InitObjCallback();
		
		//
		AnalysisID(in.property["objectID"]);		
	}
	
	CObject::CObject()
	{
		InitObjCallback();
	}
	
	bool CObject::InitObjCallback()
	{
		sGlobal.RequestCallbackMap["数据增加"] = CObject::ObjectCallbackSqlAdd;
		sGlobal.RequestCallbackMap["数据修改"] = CObject::ObjectCallbackSqlEdit;
		sGlobal.RequestCallbackMap["数据删除"] = CObject::ObjectCallbackSqlDel;
		return true;
	}
	
	bool CObject::AnalysisID(string id)
	{
		
		if(id.length() == OBJECT_ID_LENGTH)
		{
			int pos = 0;
			
			nValues["对象类型编码"] = id.substr(pos,OBJECT_CODE_LENGTH);			
			
			pos += OBJECT_CODE_LENGTH;
			
			nValues["手机号码"] = id.substr(pos,OBJECT_PHONE_LENGTH);
			nValues["对象索引"] = id.substr(pos,OBJECT_ID_LENGTH - OBJECT_CODE_LENGTH);

			//读取序列号
			pos += OBJECT_PHONE_LENGTH;
			nValues["对象序号"] = id.substr(pos,OBJECT_SERIAL_LENGTH);
			nValues["对象编码"] = id;
			
		//	cout<<"SerialCode:"<<nValues["SerialCode"]<<endl;
		}
		else
		{
			cout<<DEBUG_POSTION<<"字符串不符合规范长度:"<<OBJECT_ID_LENGTH<<" 实际长度为:"<<id.length()<<endl;
			return false;
		}
		
		return true;
	};

	CObject::~CObject()
	{

	}
	
	
	//获取该对象的编码属性
	//codeType:可取以下值
	//TypeCode:类型编码
	//ProvinceCode:省编码
	//CountyCode:县编码
	//AreaCode:区域编码
	//SerialCode:序列号编码·1111111111111																11

	const string CObject::Code(string codeType)
	{
		if(nValues.find(codeType) != nValues.end())
		{
			return nValues[codeType];
		}
		return "";
	}

	//获取制定信号类型的信号列表
	const map<string,map<string,string > > CObject::GetSemTypeIDs(int semType)
	{
		map<string,map<string,string > > ret;

		return ret;
	}
	
	const list<TObjectProperty>& CObject::Out()
	{
		//list<TObjectProperty> l;
		//l.push_back(mObjectProperty);
		return mOutObjList;
	}
	CObject::ObjectPtr CObject::Copy()
	{
		//cout<<"CObject Copy"<<endl;
		ObjectPtr oPtr(new CObject(mObjectProperty));
		return oPtr;
	}
	
	CObject& CObject::operator = (const CObject &d)
	{
		nValues = d.nValues;
		mObjectProperty = d.mObjectProperty;
		
	}
	
	string CObject::GetObjectType()
	{
		return nValues["对象类型"];
	}
	
	int CObject::CodeInt(string codeType)
	{
		if(nValues.find(codeType) != nValues.end())
		{
			return boost::lexical_cast<int>(nValues[codeType]);;
		}
		return -1;
	}

	bool CObject::LoadPropertCfg(string path)	//加载配置文件目录,一般在初始化的时候使用
	{
		using namespace boost::property_tree;
		
		bool ret = true;
		try 
		{		
			string filePath = path + "/";
			filePath += nValues["对象类型"];
			filePath += ".json";
			ptree root;			

			ptree objects;
			read_json(filePath,root);
		//	cout<<"加载配置文件目录:"<<filePath<<endl;
		
			ptree charMapList = root.get_child("字段映射表");
			ptree::iterator charMaplIter = charMapList.begin();
			nValues["数据表"] = root.get<string>("表");
			string objType = nValues["对象类型"];//root.get<string>("表");
			if(objType != "")
			{
				for(; charMaplIter != charMapList.end(); ++charMaplIter)
				{
					ptree::iterator charlIter;
					string semName = charMaplIter->second.get<string>("信号量");
					
					for(charlIter = charMaplIter->second.begin();charlIter != charMaplIter->second.end();++charlIter)
					{
						sGlobal.DatabaseCfg.tableProperty[objType][semName][charlIter->first] = charMaplIter->second.get<string>(charlIter->first);
					}
				}
			}
			ptree requestList = root.get_child("请求列表");
			ptree::iterator rstlIter = requestList.begin();
			for(; rstlIter != requestList.end(); ++rstlIter)
			{
				string request = rstlIter->second.get<string>("请求");
				mObjectCtrlCfgs[request].property["请求"] = rstlIter->second.get<string>("请求");
				mObjectCtrlCfgs[request].property["基础函数"] = rstlIter->second.get<string>("基础函数");
				
				ptree property =  rstlIter->second.get_child("属性");
				ptree::iterator pptylIter;
				for(pptylIter = property.begin();pptylIter != property.end();++pptylIter)
				{
					mObjectCtrlCfgs[request].property[pptylIter->first] = property.get<string>(pptylIter->first);					
					//cout<<pptylIter->first<<":"<<property.get<string>(pptylIter->first)<<endl;
				}				
				
				//重新打包生成反馈函数
				//读取obj的id
				//读取obj的requstID
				//读取semaphoreIDs下面的信号列表
		
		 
			}

		} 
		catch (boost::exception &e) 
		{
			TRACE(RSTR("boost错误:")"%s\n",boost::diagnostic_information(e).c_str());//boost::diagnostic_information 会提取所有信息
			ret = false;
		} 
		catch(std::exception& e){//catch的前后顺序定义会有区别抓取的

			TRACE(RSTR("系统错误:")"%s\n",e.what());	
			ret = false;
		}
		
		return ret;
	}
	
	bool CObject::LoadDicSPID(string request,string replyType,string semID)
	{
		mObjectCtrlCfgs[request].sems[replyType][semID] = semID;		
		//cout<<"增加请求量:"<<request<<" 类型:"<<replyType<<" 信号ID:"<<semID<<endl;
		return true;
	}
	
	bool CObject::AttachObjectProperty(TObjectProperty in)
	{
		mObjectProperty= in;
		//分析对象ID
		AnalysisID(in.property["objectID"]);
		
		//分析requestID
		AnalysisDatabaseKey();
		
		
		return true;
	}
	
	bool CObject::Go()
	{
		string request;
		if(sGlobal.GetDicInfo(nValues["对象类型"],mObjectProperty.property["requestID"],"基础函数",request))
		{
			map<string,DEF_REQUEST_CALLBACK>::iterator flIter = sGlobal.RequestCallbackMap.find(request);
				
			if(flIter != sGlobal.RequestCallbackMap.end())
			{
				if(flIter->second != NULL)
				{
					return flIter->second(this);
				}
				else
				{
					boost::throw_exception(runtime_error("请求量未实现基础函数"));
				}
			}
			else
			{
				boost::throw_exception(runtime_error("请求量未正确对应基础函数"));
			}
			
		}
		else
		{
			if(mObjectProperty.property["requestID"] == "")
			{
				//查询关键字
				if(CodeInt("对象序号") == 0)
				{
					ObjectCallbackSqlSearch(this);
				}
				else
				{
					ObjectCallbackSqlInquiry(this);
				}
			}
		}
		//根据请求量读取基础函数
		//判断是否存在基础函数
		//
		//mObjectCtrlCfgs.
		
		return false;
	}

	string CObject::GetProperty(string key)
	{
		string ret = "";
		map<string,string>::iterator lIter;
		lIter = nValues.find(key);
		if(lIter != nValues.end())
		{
			return lIter->second;
		}
			
		
		return ret;
	}
	bool CObject::AnalysisObject(map<string,map<string,string> > & tables,map<string,map<string,string> > & extTable)
	{
		//map<string,string> tables;
		
		map<int, map<int,map<string,string> > >::iterator lIter;
		
		string requestID = mObjectProperty.property["requestID"];
		string necessarySemIDs,optionSemIDs;
		string objType = nValues["对象类型"];
		sGlobal.GetDicInfo(objType,requestID,"必要的信号量",necessarySemIDs);
		sGlobal.GetDicInfo(objType,requestID,"可选的信号量",optionSemIDs);
		
		map<string,string> necSems;
		for(lIter = mObjectProperty.spArr.begin();lIter != mObjectProperty.spArr.end();++lIter)
		{
			map<int,map<string,string> >::iterator splIter;
			for(splIter = lIter->second.begin();splIter != lIter->second.end();++splIter)
			{
				string label;
				string id = splIter->second["ID"];
				
				string semType;
				if(sGlobal.GetSemType(id.substr(2,2),semType))
				{
					string semName;
					//读取信号名
					sGlobal.GetDicInfo(objType,id,"信号名",semName);
					if(necessarySemIDs.find(semName) == string::npos)
					{
						if(optionSemIDs.find(semName) == string::npos && semType != "扩展量")
						{
							string sMsg;
							sMsg = "传进来的信号量存在非法的信号量【";
							sMsg += semName;
							sMsg += "】";
							boost::throw_exception(runtime_error(sMsg));
						}
					}
					else
					{
						necSems[semName] = semName;
					}
					if(semType == "索引量" || semType == "记录量")
					{				
				
						
						if(sGlobal.GetDicInfo(objType,id,"字段名",label))
						{
							tables[label]["信号值"] = splIter->second["value"];
							tables[label]["信号ID"] = id;
							sGlobal.GetDicInfo(objType,id,"信号名",tables[label]["信号名"]);
						}
					}
					else if(semType == "扩展量")
					{
						
						label = id;
						extTable[label]["sn"] = id.substr(0,SEM_SN_LENGTH);
						extTable[label]["serial"] = id.substr(SEM_SN_LENGTH,SEM_SERIAL_LENGTH);
						extTable[label]["信号值"] = splIter->second["value"];
						extTable[label]["信号ID"] = id;
						if(splIter->second.find("flag4") != splIter->second.end())
						{
							extTable[label]["扩展表"] = splIter->second["flag4"];
						}
						else
						{
							extTable[label]["扩展表"] = "TextExt";
						}
					}						
				}				
			}			
		}
		
		int cnt = 0;
		if(necessarySemIDs != "")
		{
			cnt = count(necessarySemIDs.begin() , necessarySemIDs.end() , ';') + 1;
		}
		if(necSems.size() != cnt)
		{
			//必要的信号量全部都有了;
			boost::throw_exception(runtime_error((boost::format("缺少必要的信号量，其中必要的信号量数量【%d】,实际【%d】")%cnt%necSems.size()).str()));
		}
		
		return true;
	}
	
	//map<string,map<string,string> > 
	
	bool CObject::AnalysisInquiryObject(map<string,map<string,string> >& tables,map<string,map<string,string> >& extTable )
	{
		//map<string,map<string,string> > tables;

		map<int, map<int,map<string,string> > >::iterator lIter;

		string objType = nValues["对象类型"];

		for(lIter = mObjectProperty.spArr.begin();lIter != mObjectProperty.spArr.end();++lIter)
		{
			map<int,map<string,string> >::iterator splIter;
			for(splIter = lIter->second.begin();splIter != lIter->second.end();++splIter)
			{
				string label;
				string id = splIter->second["ID"];
				
				string semType;
				if(sGlobal.GetSemType(id.substr(2,2),semType))
				{
					if(semType == "索引量" || semType == "记录量")
					{
						//读取信号名
						if(sGlobal.GetDicInfo(objType,id,"字段名",label))
						{					
							tables[label]["信号值"] = splIter->second["value"];
							tables[label]["信号ID"] = id;
							sGlobal.GetDicInfo(objType,id,"信号名",tables[label]["信号名"]);
						}
						else
						{
							boost::throw_exception(runtime_error(
												(boost::format("信号量[%s]无法在字典表里找到")%id).str()));
						}
					}
					else if(semType == "扩展量")
					{
						label = id;
						extTable[label]["sn"] = id.substr(0,SEM_SN_LENGTH);
						extTable[label]["serial"] = id.substr(SEM_SN_LENGTH,SEM_SERIAL_LENGTH);
						extTable[label]["信号值"] = splIter->second["value"];
						extTable[label]["信号ID"] = id;
						if(splIter->second.find("flag4") != splIter->second.end())
						{
							extTable[label]["扩展表"] = splIter->second["flag4"];
						}
						else
						{
							extTable[label]["扩展表"] = "TextExt";
						}
					}
					else
					{
						boost::throw_exception(runtime_error(
												(boost::format("信号类型%s[%s]无法作为查询参数")%semType%id).str()));
					}

				}
				else
				{
					boost::throw_exception(runtime_error(
												(boost::format("无法识别信号类型编码[%s]")%id.substr(2,2)).str()));
				}
			}			
		}		
		
		return true;
	}
	
	bool CObject::ObjectCallbackSqlAddOrEdit(CObject* obj)
	{
		map<string,map<string,string> > tables;
		map<string,map<string,string> > extTables;
		string sqlStr;
		string values;
		string clsNames;
		string up;
		
		if(obj->AnalysisObject(tables,extTables))
		{		
			map<string,map<string,string> >::iterator tlIter;
			for(tlIter = tables.begin();tlIter != tables.end();++tlIter)
			{
				if(clsNames != "")
				{
					clsNames += ",";
				}
				if(values != "")
				{
					values += ",";
				}
				if(up != "")
				{
					up += ",";
				}
				clsNames += tlIter->first;
				values += "'";
				values += tlIter->second["信号值"];
				values += "'";
				
				up += tlIter->first;
				up += "='";
				up += tlIter->second["信号值"];
				up += "'";
			}
			
			if(clsNames != "" && values != "")
			{
				sqlStr = (boost::format("insert into %s(%s,id) values(%s,'%s') ON DUPLICATE KEY UPDATE id = '%s',%s;")%sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"]%clsNames%values%obj->nValues["对象索引"]%obj->nValues["对象索引"]%up).str();	
				obj->mOutObjList.push_back(obj->mObjectProperty);
				//cout<<sqlStr<<endl;
				if(!obj->mDatabasePtr->Execute(sqlStr))
				{
					boost::throw_exception(runtime_error("数据操作时发生错误"));
				}
			}
			
			/*
			
				extTable[label]["sn"] = id.substr(0,SEM_SN_LENGTH);
				extTable[label]["serial"] = id.substr(SEM_SN_LENGTH,SEM_SERIAL_LENGTH);
				extTable[label]["信号值"] = splIter->second["value"];
				extTable[label]["信号ID"] = id;
			*/
			
			for(tlIter = extTables.begin();tlIter != extTables.end();++tlIter)
			{
				string tableName = sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"] + tlIter->second["扩展表"];
				string sn = tlIter->second["sn"];
				string serial = tlIter->second["serial"];
				string val = tlIter->second["信号值"];
				string matchID = obj->nValues["对象索引"];
				
				sqlStr =  (boost::format("insert into %s(matchID,semSN,semSerial,val) select '%s','%s','%s','%s' from dual where not exists(select id from %s where matchID=%s and semSN=%s and semSerial=%s)")%tableName%matchID%sn%serial%val%tableName%matchID%sn%serial).str();
				if(!obj->mDatabasePtr->Execute(sqlStr))
				{
					boost::throw_exception(runtime_error("数据扩展操作时发生错误"));
				}
			}
			
			return true;
			
		}
		boost::throw_exception(runtime_error("分析对象时发生错误"));

		return false;
	}
	
	//增加
	bool CObject::ObjectCallbackSqlAdd(CObject* obj)
	{
		string sqlStr;
		string values;
		string clsNames;
		map<string,map<string,string> > tables;
		map<string,map<string,string> > extTables;

		//tables = obj->AnalysisObject();
		if(obj->AnalysisObject(tables,extTables))
		{
			map<string,map<string,string> >::iterator tlIter;
			for(tlIter = tables.begin();tlIter != tables.end();++tlIter)
			{
				if(clsNames != "")
				{
					clsNames += ",";
				}
				if(values != "")
				{
					values += ",";
				}

				clsNames += tlIter->first;
				values += "'";
				values += tlIter->second["信号值"];
				values += "'";
			}
			
			
			if(clsNames != "" && values != "")
			{			
				sqlStr = (boost::format("insert into %s(%s,id) values(%s,'%s');")%sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"]%clsNames%values%obj->nValues["对象索引"]).str();	
				obj->mOutObjList.push_back(obj->mObjectProperty);
				//cout<<sqlStr<<endl;
				if(!obj->mDatabasePtr->Execute(sqlStr))
				{
					boost::throw_exception(runtime_error("增加数据扩展操作时发生错误"));
				}
			}
			
			for(tlIter = extTables.begin();tlIter != extTables.end();++tlIter)
			{
				string tableName = sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"] + tlIter->second["扩展表"];
				string sn = tlIter->second["sn"];
				string serial = tlIter->second["serial"];
				string val = tlIter->second["信号值"];
				string matchID = obj->nValues["对象索引"];
				
				sqlStr =  (boost::format("insert into %s(matchID,semSN,semSerial,val) valuex(%s','%s','%s','%s')")%tableName%matchID%sn%serial%val%values).str();
				if(!obj->mDatabasePtr->Execute(sqlStr))
				{
					boost::throw_exception(runtime_error("数据扩展操作时发生错误"));
				}
			}
			return true;
			
		}
		
		boost::throw_exception(runtime_error("在插入数据时发现没有传入一个可用的信号量"));
		
		return false;
	}
	//修改
	bool CObject::ObjectCallbackSqlEdit(CObject* obj)
	{
		map<string,map<string,string> > tables;
		map<string,map<string,string> > extTables;
		string sqlStr;
		string up;
		
		if(obj->AnalysisObject(tables,extTables))
		{		
			map<string,map<string,string> >::iterator tlIter;
			for(tlIter = tables.begin();tlIter != tables.end();++tlIter)
			{
				if(up != "")
				{
					up += ",";
				}

				
				up += tlIter->first;
				up += "='";
				up += tlIter->second["信号值"];
				up += "'";
			}
			if(up != "")
			{
				sqlStr = (boost::format("update %s set %s where id='%s'")%sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"]%up%obj->nValues["对象索引"]).str() ;
				obj->mOutObjList.push_back(obj->mObjectProperty);
				if(!obj->mDatabasePtr->Execute(sqlStr))
				{
					boost::throw_exception(runtime_error("数据扩展操作时发生错误"));
				}
			}
			for(tlIter = extTables.begin();tlIter != extTables.end();++tlIter)
			{
				
				string tableName = sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"] + tlIter->second["扩展表"];
				string sn = tlIter->second["sn"];
				string serial = tlIter->second["serial"];
				string val = tlIter->second["信号值"];
				string matchID = obj->nValues["对象索引"];
				
				sqlStr =  (boost::format("update %s set val='%s' where matchID='%s' and semSN='%s' and semSerial='%s'")%tableName%val%matchID%sn%serial).str();
				if(!obj->mDatabasePtr->Execute(sqlStr))
				{
					boost::throw_exception(runtime_error("数据扩展操作时发生错误"));
				}
			}
			return true;
		}
		boost::throw_exception(runtime_error("在更新数据时发现没有传入一个可用的信号量"));
		return false;
	}
		//删除
	bool CObject::ObjectCallbackSqlDel(CObject* obj)
	{
		string sqlStr;
		//cout<<__FUNCTION__<<endl;

		sqlStr = (boost::format("delete from %s where id='%s';")%sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"]%obj->nValues["对象索引"]).str() ;	
		obj->mDatabasePtr->Execute(sqlStr);
		sqlStr = (boost::format("delete from %s where matchID='%s';")%(sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"]+"TextExt")%obj->nValues["对象索引"]).str();
		obj->mDatabasePtr->Execute(sqlStr);
		sqlStr = (boost::format("delete from %s where matchID='%s';")%(sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"]+"NumExt")%obj->nValues["对象索引"]).str();
		obj->mDatabasePtr->Execute(sqlStr);		
		obj->mOutObjList.push_back(obj->mObjectProperty);
		return true;
	}
	//搜索
	bool CObject::ObjectCallbackSqlSearch(CObject* obj)
	{
		string sqlStr;
		string clsNames;
		string condition;
		string extCondition;
		int start = 0;
		int count = 10;
		
		//序号为0的为查询量的关键字，不为0的的需要返回的量
		
		map<int, map<int,map<string,string> > >::iterator lIter;

		string objType = obj->nValues["对象类型"];
		map<string,string> idClsMap;	//信号ID和字段的映射关系
		
		if(obj->mObjectProperty.property.find("countPerPage") != obj->mObjectProperty.property.end()
			&& obj->mObjectProperty.property.find("page") != obj->mObjectProperty.property.end()
		)
		{
			count = boost::lexical_cast<int>(obj->mObjectProperty.property["countPerPage"]);
			start = boost::lexical_cast<int>(obj->mObjectProperty.property["page"]) * count;
		}
		
		for(lIter = obj->mObjectProperty.spArr.begin();lIter != obj->mObjectProperty.spArr.end();++lIter)
		{
			map<int,map<string,string> >::iterator splIter;
			for(splIter = lIter->second.begin();splIter != lIter->second.end();++splIter)
			{
				string label;
				string id = splIter->second["ID"];
				string semType;
				if(sGlobal.GetSemType(id.substr(2,2),semType))
				{
					map<string,string> semProperty;
					semProperty["sn"] = id.substr(0,SEM_SN_LENGTH);
					semProperty["serial"] = id.substr(SEM_SN_LENGTH,SEM_SERIAL_LENGTH);
					sGlobal.GetDicInfo(objType,id,"字段名",label);
					if(splIter->first == 0)
					{						
						string formulaType = "";
						
						if(semType == "扩展量")
						{
							formulaType = "扩展数据库公式";
						}
						else if(semType == "记录量" || semType == "索引量")
						{
							formulaType = "公式";
						}

						//读取操作符
						string instSign = splIter->second["flag1"];
						
						string formula;
						//读取公式
						if(sGlobal.GetFormula(formulaType,instSign,formula))
						{
							//查询条件
							if(condition != "")
							{
								condition += " and ";
								
							}
							//读取公式成功
							string value;
							
							formula = Common::ReplaceString(formula,"{{字段}}",label);
							while(Common::ExtractVariableFromFormula(formula,value))
							{
								if(splIter->second.find(value) != splIter->second.end())
								{
									formula = Common::ReplaceString(formula,"{{"+value+"}}",splIter->second[value]);
								}
								else
								{
									if(obj->nValues.find(value) != obj->nValues.end())
									{
										formula = Common::ReplaceString(formula,"{{"+value+"}}",obj->nValues[value]);
									}
									else
									{
										if(semProperty.find(value) != semProperty.end())
										{
											formula = Common::ReplaceString(formula,"{{"+value+"}}",semProperty[value]);
										}
										else
										{
											if(sGlobal.Dic[obj->nValues["对象类型"]].property.find(value) != sGlobal.Dic[obj->nValues["对象类型"]].property.end())
											{
												formula = Common::ReplaceString(formula,"{{"+value+"}}",sGlobal.Dic[obj->nValues["对象类型"]].property[value]);
											}
											else
											{
												boost::throw_exception(runtime_error(
												(boost::format("发现变量【%s】没有被解析")%value).str()));
											}
										}
									}
									
								}
							}
							condition += formula;
						}
						else
						{
							boost::throw_exception(runtime_error(
											(boost::format("获取公式失败,类型:【%s】,标志:【%s】")%formulaType%instSign).str()));
						}
					}
					else
					{
						if(semType == "记录量" || semType == "索引量")
						{
							
							if(label != "")
							{
								if(clsNames != "")
								{
									clsNames += ",";
								}
								clsNames += label;
								idClsMap[label] = id;	//将进行查询的字段记录下来
							}
							else
							{
								boost::throw_exception(runtime_error(
											(boost::format("发现id【%s】无法对应到任何字段")%id).str()));
							}
							
						}
						else
						{
							boost::throw_exception(runtime_error(
											(boost::format("发现id【%s】无法被搜索查询【%s】")%id%semType).str()));
						}
					}
				}
				else
				{
					boost::throw_exception(runtime_error(
											(boost::format("无法获取%s的信号类型")%id).str()));
				}
			}			
		}
		
		
		if(clsNames != "" && condition != "")
		{
			if(idClsMap.find("id") == idClsMap.end())
			{
				clsNames += ",id";
			}
			sqlStr = (boost::format("select %s from %s where %s limit %d,%d")%clsNames%sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"]%condition%start%count).str();
					
			cout<<RSTR("形成的查询语句:")<<sqlStr<<endl;
			
			list<map<string,string> > infoList;
			
			
			if(obj->mDatabasePtr->SearchList(sqlStr, infoList))
			{
				list<map<string,string> >::iterator rslIter;
				string  time = Common::GetLocalTime();
				for(rslIter = infoList.begin();rslIter != infoList.end();++rslIter)
				{
					string objCode = obj->Code("对象类型编码");
					objCode += Common::FixString((*rslIter)["id"],OBJECT_PHONE_LENGTH + OBJECT_SERIAL_LENGTH);
					
					TObjectProperty obPro;
					obPro.property["objectID"] = objCode;
					obPro.property["requestID"] = "";
					
					map<string,string>::iterator mlIter;
					for(mlIter = idClsMap.begin();mlIter != idClsMap.end();++mlIter)
					{
						if(idClsMap.find(mlIter->first) != idClsMap.end())
						{
							string id = idClsMap[mlIter->first];
							string value = (*rslIter)[mlIter->first];
							int sn = boost::lexical_cast<int>(id.substr(0,SEM_SN_LENGTH));
							int serial = boost::lexical_cast<int>(id.substr(SEM_SN_LENGTH,SEM_SERIAL_LENGTH));
							obPro.spArr[sn][serial]["ID"] = id;
							obPro.spArr[sn][serial]["value"] = value;
							obPro.spArr[sn][serial]["time"] = time;
						}
					}/**/
					obj->mOutObjList.push_back(obPro);
				}
			}
			
			return true;
		}
		else
		{
			boost::throw_exception(runtime_error((boost::format("查询条件[%s],内容[%s]均不能为空")%condition%clsNames).str()));
		}
		
		return false;
	}
	//查询
	bool CObject::ObjectCallbackSqlInquiry(CObject* obj)
	{
		string sqlStr;
		string clsNames;
		map<string,map<string,string> > tables;
		map<string,map<string,string> > extTables;
		//序号为0的为查询量的关键字，不为0的的需要返回的量
		
		if(obj->AnalysisInquiryObject(tables,extTables))
		{
			map<string,map<string,string> > sqlStrs;
			map<string,map<string,string> >::iterator lIter;
			for(lIter = tables.begin();lIter != tables.end();++lIter)
			{
				if(clsNames != "")
				{
					clsNames += ",";
				}
				clsNames+= lIter->first;
			}
			if(extTables.size() > CONF_MAX_EXT_TABLE_SEM_COUNT)
			{
				boost::throw_exception(runtime_error((boost::format("扩展量数量[%d]高于系统预制的最大扩展量数[%d]")%extTables.size()%CONF_MAX_EXT_TABLE_SEM_COUNT).str()));
			}
			for(lIter = extTables.begin();lIter != extTables.end();++lIter)
			{
				string extTable = lIter->second["扩展表"];
				if(sqlStrs[extTable]["condition"] != "")
				{
					sqlStrs[extTable]["condition"] += " or (";
				}
				sqlStrs[extTable]["condition"] += ("semSN=" + lIter->second["sn"]);
				if(boost::lexical_cast<int>(lIter->second["serial"]) != 0)
				{
					sqlStrs[extTable]["condition"] += (" and serial=" + lIter->second["serial"]);
				}
				sqlStrs[extTable]["condition"] += ")";
			}
			TObjectProperty obPro;
			obPro.property["objectID"] = obj->Code("对象编码");;
			obPro.property["requestID"] = "";
			if(clsNames != "")
			{
				string condition = (boost::format("id='%s'")%obj->nValues["对象索引"]).str();
				sqlStr = (boost::format("select %s from %s where %s limit 0,1")%clsNames%sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"]%condition).str();
						
				cout<<RSTR("形成的查询语句:")<<sqlStr<<endl;
				
				map<string,string>  info;
				
				
				if(obj->mDatabasePtr->Detail(sqlStr, info))
				{
					string  time = Common::GetLocalTime();
					


					map<string,map<string,string> >::iterator mlIter;
					for(mlIter = tables.begin();mlIter != tables.end();++mlIter)
					{
						string id = mlIter->second["信号ID"];
						string value = info[mlIter->first];
						int sn = boost::lexical_cast<int>(id.substr(0,SEM_SN_LENGTH));
						int serial = boost::lexical_cast<int>(id.substr(SEM_SN_LENGTH,SEM_SERIAL_LENGTH));
						obPro.spArr[sn][serial]["ID"] = id;
						obPro.spArr[sn][serial]["value"] = value;
						obPro.spArr[sn][serial]["time"] = time;					
					}				
					
									
				}				
			}
			
			map<string,map<string,string> >::iterator sqllIter;
			for(sqllIter = sqlStrs.begin();sqllIter != sqlStrs.end();++sqllIter)
			{
				string condition = " matchID=" + obj->mObjectProperty.property["对象编码"];
				condition += " and (";
				condition += sqllIter->second["condition"];
				condition += ")";
				sqlStr = (boost::format("select * from %s%s where %s limit 0,%d")%sGlobal.Dic[obj->nValues["对象类型"]].property["数据表名"]%sqllIter->first%condition%CONF_MAX_EXT_TABLE_SEM_COUNT).str();
				list<map<string,string> > infoList;
			
			
				if(obj->mDatabasePtr->SearchList(sqlStr, infoList))
				{
					list<map<string,string> >::iterator rslIter;
					string  time = Common::GetLocalTime();
					for(rslIter = infoList.begin();rslIter != infoList.end();++rslIter)
					{
						map<string,string>::iterator mlIter;
	
						string value = (*rslIter)["value"];
						int sn = boost::lexical_cast<int>((*rslIter)["semSN"]);
						int serial = boost::lexical_cast<int>((*rslIter)["semSerial"]);
						obPro.spArr[sn][serial]["ID"] = (boost::format("%s%s%s")%Common::FixString(obj->nValues["对象类型编码"],2)%Common::FixString((*rslIter)["semSN"],SEM_SN_LENGTH)%Common::FixString((*rslIter)["semSerial"],SEM_SERIAL_LENGTH)).str();
						obPro.spArr[sn][serial]["value"] = value;
						obPro.spArr[sn][serial]["time"] = time;	

					}
				}
				
			}
			
			obj->mOutObjList.push_back(obPro);
			
			return true;
		}
		
		return false;
	}
	
	bool CObject::AttachDatabase(DatabasePtr& pData)
	{
		mDatabasePtr  = pData;
		return true;
	}
	
	bool CObject::AnalysisDatabaseKey()
	{
		nValues["数据库关键字"] = nValues["手机号码"].substr(0,3);	//取手机号码的前3位作为数据库索引
		
		return true;
	}
	
};